package util;

public class TpsCounter {
	  long[] times;
	  long lastTick;
	  int ptr;

	  public TpsCounter(int bufferSize) {
	    times = new long[bufferSize];
	    ptr = 0;
	    lastTick = System.currentTimeMillis();
	  }
	  
	  public void tick() {
	    ptr = (ptr+1) % times.length;
	    times[ptr] = System.currentTimeMillis()-lastTick; 
	    lastTick = System.currentTimeMillis();
	  }
	  
	  public float get() {
	    int timeSum = 0;
	    for (int i=0; i<times.length; i++)
	      timeSum+=times[i];
	    return (1.0f/((float)timeSum/(float)times.length))*1000.0f;
	  }
	  
	  public String toString() {
		return Double.toString(Math.ceil(get() * 100) / 100);  
	  }
}
